package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.LogisticsPriceDTO;
import com.itheima.pojo.LogisticsPrice;
import com.itheima.service.LogisticsPriceService;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logistics/price")
public class LogisticsPriceController {

    @Autowired
    private LogisticsPriceService logisticsPriceService;

    @GetMapping("/page")
    public Page<LogisticsPriceDTO> getLogisticsPricePage(Page<LogisticsPriceDTO> page, LogisticsPriceDTO dto) {
        return logisticsPriceService.getLogisticsPricePage(page, dto);
    }

    @GetMapping("/list")
    public Result list() {
        return Result.success(logisticsPriceService.listAll());
    }

    @GetMapping("/{id}")
    public LogisticsPrice getLogisticsPriceById(@PathVariable Long id) {
        return logisticsPriceService.getLogisticsPriceById(id);
    }

    /**
     * 查询物流公司关联的价格列表
     * @param companyId 物流公司ID
     * @return 价格列表
     */
    @GetMapping("/company/{companyId}")
    public Result queryPricesByCompany(@PathVariable Integer companyId) {
        List<LogisticsPriceDTO> prices = logisticsPriceService.queryPricesByCompany(companyId);
        return Result.success(prices);
    }

    @PostMapping
    public Result saveLogisticsPrice(@RequestBody LogisticsPrice logisticsPrice) {
        return Result.success(logisticsPriceService.saveLogisticsPrice(logisticsPrice));
    }

    @PutMapping
    public Result updateLogisticsPrice(@RequestBody LogisticsPrice logisticsPrice) {
        return Result.success(logisticsPriceService.updateLogisticsPrice(logisticsPrice));
    }

    @DeleteMapping("/{id}")
    public Result deleteLogisticsPrice(@PathVariable Long id) {
        return Result.success(logisticsPriceService.deleteLogisticsPrice(id));
    }

    /**
     * 批量保存物流价格
     * @param prices 价格列表
     * @param companyId 物流公司ID
     * @return 操作结果
     */
    @PostMapping("/batch")
    public Result batchSavePrices(
            @RequestBody List<LogisticsPriceDTO> prices,
            @RequestParam Integer companyId
    ) {
        boolean result = logisticsPriceService.batchSavePrices(companyId, prices);
        return Result.success(result);
    }
}